package com.gaming.glb;

import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.opengl.texture.region.BaseTextureRegion;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;

public class CatEndGame extends Cat {

	public CatEndGame(Scene scene, BaseTextureRegion pBallTextureRegion, float posX, float posY, PhysicBall ball) {
		super(scene, pBallTextureRegion, ball);
		
		super.newEntity = new AnimatedSprite(posX, posY + (super.height / 3), (TiledTextureRegion) super.baseTextureRegion);
	}
	
	@Override
	public void attachToScene(Scene scene) {		
    	scene.attachChild(this.newEntity);    	
    	
    	 scene.registerUpdateHandler(new IUpdateHandler(){
 			@Override
 			public void reset() { }

 			@Override
 			public void onUpdate(final float pSecondsElapsed) {
 				
 				if(canBeCatched()) {
     	        	((AnimatedSprite)CatEndGame.this.getSprite()).animate(new long[]{50, 50, 50}, 0, 2, false);
     	        }
 				
				if(isCached()) 
				{	
					if (CatEndGame.this.mBall.isMoving()) 
 					{							
						CatEndGame.this.mBall.stopMoving();
						((AnimatedSprite)CatEndGame.this.getSprite()).animate(new long[]{0, 5, 15, 25}, 2, 5, false);
 					} 					
     	        }
			}

			private boolean isCached() {
				return CatEndGame.this.mBall != null && CatEndGame.this.mBall.getSprite() != null && CatEndGame.this.mBall.getSprite().collidesWith(CatEndGame.this.getSprite());
			}

			private boolean canBeCatched() {
				return CatEndGame.this.mBall != null && CatEndGame.this.mBall.getSprite() != null && 
						CatEndGame.this.mBall.getSprite().getX() <= CatEndGame.this.getSprite().getX() - 60F &&
					    (CatEndGame.this.mBall.getSprite().getY() >= CatEndGame.this.getSprite().getX() - 1F ||
					     CatEndGame.this.mBall.getSprite().getY() <= CatEndGame.this.getSprite().getX() + 0F );
			} 			
		}); 	
	}

}
